package com.allcontroller;

import com.wzy.pojo.Book4;
import com.wzy.pojo.Data4;
import com.wzy.pojo.User4;
import com.wzy.pojo.User4;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.ArrayList;

@Controller
@RequestMapping("book")
public class Controller4 {
    ArrayList<Book4> books = Data4.getData();
    @RequestMapping("/list")
    public String findBook(Model model , HttpSession httpSession){
        httpSession.setAttribute("user",new User4("wzy","ADMIN"));
        model.addAttribute("books",books);
        return "books";
    }

    @CrossOrigin
    @PostMapping("/search")
    public String searchBook(@RequestParam("name") String bname,
                             @RequestParam("author") String batuhor , Model model, HttpSession session){
        ArrayList<Book4> bs = new ArrayList<>();
//        String bname = book.getName();
//        String batuhor = book.getAuthor();

//        System.out.println("search方法----  name="+bname+"  author="+batuhor);

        if(bname.isEmpty() && batuhor.isEmpty()) {
            bs = books;
        }else {
            for(Book4 b:books){
                if(!bname.isEmpty() && b.getName().contains(bname) ||
                        (!batuhor.isEmpty() && b.getAuthor().contains(batuhor))
                ){
                    bs.add(b);
                }

            }
        }
        session.setAttribute("user",new User4("wzy","admin"));
        model.addAttribute("books",bs);
        return "books";
    }
    @RequestMapping("find/{id}")
    public String findBook(@PathVariable("id") Integer id){
        System.out.println("申请借阅图书的id"+id);
        return "books";
    }
}